package com.yang.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.yang.springcloud.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class StudentConsumerConterller {

    @Autowired
    private RestTemplate rte;

//    private static final String REST_URL_PREFIX = "http://127.0.0.1:7071";
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-STUDENT";

    @PostMapping("/consumer/student/insert")
//    @HystrixCommand(fallbackMethod = "hystrixAddStudent", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000" )
//    })
    public Map<String, Object> addStudent(Student s){
//        if(s.getId() == null || s.getName() == null || s.getBirth() == null || s.getSex() == null){
//            throw new RuntimeException("参数不能为空");
//        }

        System.out.println(s.getId()+ " " + s.getName() + " " + s.getBirth() + " " + s.getSex());
        return rte.postForObject(REST_URL_PREFIX+"/insert", s, Map.class);
    }

//    public Map<String, Object> hystrixAddStudent(Student s){
//        Map<String, Object> msg = new HashMap<String, Object>();
//        msg.put("massage", "参数不能为空");
//        msg.put("code", 400);
//        return msg;
//    }

    @PostMapping("/consumer/student/delete")
    public Map<String, Object> deleteStudent(String name){
        System.out.println(name);
        return rte.postForObject(REST_URL_PREFIX+"/delete", name, Map.class);

    }

    @PostMapping("/consumer/student/update")
    public Map<String, Object> updateStudent(Student s){
        return rte.postForObject(REST_URL_PREFIX+"/update", s, Map.class);

    }

    @GetMapping("/consumer/student/query")
    public List<Student> queryStudent(){
        return rte.getForObject(REST_URL_PREFIX+"/query",List.class);

    }
}
